/**
 * 
 */
package info.niwota.webime.http;

import java.util.Random;

/**
 * @author qiangli
 *
 */
public class Session {
	
	private static final int ACCESS_PUBLIC = 3;

	public final String id = nextId();
	
	public String user;
	
	public int group = ACCESS_PUBLIC; //default access
	
	public boolean authenticated = false;
	
	public String cookies;
	
	public String data; //user session
	
	public String nonce;
	
	public final long ts = System.currentTimeMillis();
	
	private static Random rand = new Random();
	
	private static long seq = System.currentTimeMillis();
	
	private static String nextId() {
		seq++;
		String id = Long.toHexString(seq) + Long.toHexString(rand.nextLong());
		return id;
	}
	
	/**
	 * 
	 */
	public Session() {
	}

	public void invalidate() {
		//logout but keep other values
		group = ACCESS_PUBLIC;
		authenticated = false;
	}

	@Override
	public String toString() {
		return "Session [authenticated=" + authenticated + ", cookies="
				+ cookies + ", data=" + data + ", group=" + group + ", id="
				+ id + ", nonce=" + nonce + ", ts=" + ts + ", user=" + user
				+ "]";
	}


}
